﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace RFW.Silverlight.Weather
{
	public partial class DigitalNumber : UserControl
	{
		public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(int), typeof(DigitalNumber), new PropertyMetadata(new PropertyChangedCallback(onValueChanged)));

		public DigitalNumber()
		{
			InitializeComponent();
		}

		public int Value
		{
			get { return (int)this.GetValue(ValueProperty); }
			set
			{
				this.SetValue(ValueProperty, value);

				Brush off = new SolidColorBrush(Color.FromArgb(0xFF, 0xF0, 0xF0, 0xF0));

				h0.Background = off;
				h1.Background = off;
				h2.Background = off;
				v0.Background = off;
				v1.Background = off;
				v2.Background = off;
				v3.Background = off;

				DisplayDigit(this.Value);
			}
		}

		private void DisplayDigit(int value)
		{
			Brush on = new SolidColorBrush(Colors.Black);

			if (value >= 0 && value <= 9)
			{
				if (value != 1 && value != 4)
					h0.Background = on;

				if (value > 1 && value != 7)
					h1.Background = on;

				if (value != 1 && value != 4 && value != 7)
					h2.Background = on;

				if ((value < 1 || value > 3) && value != 7)
					v0.Background = on;

				if (value < 5 || value > 6)
					v1.Background = on;

				if (value == 0 || value == 2 || value == 6 || value == 8)
					v2.Background = on;

				if (value != 2)
					v3.Background = on;
			}
		}

		private static void onValueChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
		{
			if (((int)source.GetValue(e.Property)) > 9 || ((int)source.GetValue(e.Property)) < 0)
				source.SetValue(e.Property, -1);
		}
	}
}
